package com.difsonapps.android.joinme.socket;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.util.Log;

public class UpdateStatus {
	
	List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
    private final String TAG = this.getClass().getSimpleName();

	
	public UpdateStatus(String T[]){
		
		urlParameters.add(new BasicNameValuePair("id","3" ));
		urlParameters.add(new BasicNameValuePair("status", "true"  ));
		urlParameters.add(new BasicNameValuePair("ip", "100.116.127.33" ));
		urlParameters.add(new BasicNameValuePair("port", "8000" ));
	    makeRequest("http://goovemail.com:28080/joinme/rest/user/putstatus", urlParameters);

	}
	
    public  String makeRequest(String uri, List<NameValuePair> urlParameters) {
        try {
        	HttpPut httpPut = new HttpPut(uri);
        	httpPut.setEntity(new UrlEncodedFormEntity(urlParameters));
            HttpAsyncTask ttTask = new HttpAsyncTask();
            ttTask.execute(httpPut);
           return ttTask.result;
   
        } catch (Exception e) {
        	 Log.e(TAG, e.getMessage(), e.fillInStackTrace());
        }
        return null;
    }
    
    
    private class HttpAsyncTask extends AsyncTask<HttpUriRequest, Void, String> {
    	String result= null;;
    	@Override
        protected String doInBackground(HttpUriRequest... urls) {
    		BufferedReader reader=null;
            try {
            	HttpResponse response=  new DefaultHttpClient().execute(urls[0]);
            	InputStream is= response.getEntity().getContent();
            	 if(is!=null)
            	 {
            		 reader = new BufferedReader(new InputStreamReader(is));
                     StringBuilder sb = new StringBuilder();
                     String line = null;
                     while((line = reader.readLine()) != null)
                     {
                            sb.append(line + "\n");
                     }              
                     result = sb.toString();
            	 }
            	  
			} catch (Exception e) {
			Log.e(TAG, e.getMessage(), e);
			}   
            
            return result;
        }
        // onPostExecute displays the results of the AsyncTask.
        @Override
        protected void onPostExecute(String result) {
        	String id= null;
        	 Log.d(TAG,"result:" + result + "  Longeur " + result.length());
        	 
        	 try {
        		    JSONObject obj = new JSONObject(result);
        		    id= obj.getString("id");

        		} catch (Throwable t) {
        		    Log.e("My App", "Could not parse malformed JSON: \"" + result + "\"");
        		}
        	if(id!=null){        	}
  
        	
       }
    }
    

}
